package org.coolwind;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * 
 * 
 * @goal entity2service
 * @author ethan
 *
 */
@SuppressWarnings("all")
public class Entity2ServiceMojo extends AbstractMojo {
	Configuration cfg;
	/**
	 * @parameter property="entitys"
	 * @required
	 */
	private String[] entitys;
	
	/**
	 * 
	 * @parameter expression="${project.build.sourceDirectory}" 
	 */
	private String sourceDirectory;
	


	public void execute() throws MojoExecutionException, MojoFailureException {
		this.getLog().info("开始转换");
		
		try {
			cfg = new Configuration();
			cfg.setClassForTemplateLoading(this.getClass(),"/template");
			for(int i= 0;i<entitys.length;i++){
				this.createService(entitys[i]);
				this.createServiceImpl(entitys[i]);
			}
		}  catch (TemplateException e) {
			this.getLog().info(e.getMessage());
		}
		
		this.getLog().info("结束转换");
	}
	
	private void  createService(String str) throws TemplateException{
		try {
			
			Template template = cfg.getTemplate("ServiceTemplate.ftl");
			
			Map map = new HashMap();
			String entity;
			String basePackage="";
			String entityArg;
			String[] args = str.split("\\.");
			entity = args[args.length-1];
			char x= (char)(entity.charAt(0)+32);
			entityArg = entity;
			entityArg =entityArg.replace(entity.charAt(0), x);
			String path = sourceDirectory;
			for (int i = 0; i < args.length-2; i++) {
				if(i==args.length-3){
					basePackage+=args[i];
				}else{
					basePackage+=args[i]+".";
				}
				path+="\\"+args[i];
			}
			map.put("entity", entity);
			map.put("basePackage", basePackage);
			map.put("entityArg", entityArg);
			
			new File(path+"\\service").mkdirs();
			
			String fileName = path+"\\service\\"+entity+"Service.java";
			File file = new File(fileName);
			if(!file.exists()){
				template.process(map, new FileWriter(fileName));
			}
		} catch (IOException e) {
			this.getLog().info(e.getMessage());
			e.printStackTrace();
		}
	}
	
	private void  createServiceImpl(String str) throws TemplateException{
		try {
			
			Template template = cfg.getTemplate("ServiceImplTemplate.ftl");
			Map map = new HashMap();
			String entity;
			String basePackage="";
			String entityArg;
			String[] args = str.split("\\.");
			entity = args[args.length-1];
			char x= (char)(entity.charAt(0)+32);
			entityArg = entity;
			entityArg =entityArg.replace(entity.charAt(0), x);
			String path = sourceDirectory;
			for (int i = 0; i < args.length-2; i++) {
				if(i==args.length-3){
					basePackage+=args[i];
				}else{
					basePackage+=args[i]+".";
				}
				path+="\\"+args[i];
			}
			map.put("entity", entity);
			map.put("basePackage", basePackage);
			map.put("entityArg", entityArg);
			
			new File(path+"\\service\\impl").mkdirs();
			
			String fileName = path+"\\service\\impl\\"+entity+"ServiceImpl.java";
			File file = new File(fileName);
			if(!file.exists()){
				template.process(map, new FileWriter(fileName));
			}
		} catch (IOException e) {
			this.getLog().info(e.getMessage());
			e.printStackTrace();
		}
	}
}
